//
//  LoginViewController.m
//  UI豆瓣
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import "DataBaseUser.h"
#import "UserModle.h"
@interface LoginViewController ()<loginViewDelegate>
@property (nonatomic, retain)LoginView *loginV;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.loginV = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.loginV];
    [_loginV release];
    
    self.navigationItem.title = @"用户登陆";
    
    self.loginV.loginDele = self;
//    [self readNSUserDefaults];

    [[DataBaseUser shareDataBase] openDB];
    [[DataBaseUser shareDataBase] createTable];
    
}

//点击注册按钮执行协议方法
- (void)pushToRegister{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
    [registerVC release];
}
//点击登陆按钮执行协议方法
- (void)decideNameandPass{
    

    //当前登陆的用户名和密码
    self.name = self.loginV.nameTextField.text;
    self.pass = self.loginV.passwordTextField.text;
    NSLog(@"name = %@", self.name);
    NSLog(@"pass = %@", self.pass);
    
    //查找数据库里的用户名和相对应的密码
    UserModle *user = [[UserModle alloc] init];
    user = [[DataBaseUser shareDataBase] selectUserName:self.name];
    NSLog(@"======= %@", user);
    NSLog(@"++++++ name = %@", user.userName);
    NSLog(@"++++++ pass = %@", user.password);
    
    //如果当前登录的用户名和密码与数据库里的相统一,则登录成功
    if ([self.name isEqualToString:user.userName] && [self.pass isEqualToString:user.password]) {
        
        //使用NSUserDefaults,如果登陆成功,存入NSUserDefaults里两个value
        //name:用来判断收藏界面是否为当前用户名
        //isLogin = 1:用来判断当前用户为登录状态,
        NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
        [loginUserDef setObject:self.name forKey:@"name"];
        [loginUserDef setObject:@"1" forKey:@"isLogin"];
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登陆" message:@"登陆失败" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
        }];
        UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"失败");
        }];
        [alert addAction:canact];
        [alert addAction:othact];
        [self presentViewController:alert animated:YES completion:nil];
    }
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
